﻿using System.Collections.Generic;
using System.Linq;
using Runtime.Services.Battle;

namespace Runtime.Services.BattleSkill {

    /// <summary>
    /// 战斗效果过滤器
    /// </summary>
    public interface IDFR_BattleEffectFilter {

        /// <summary>
        /// 是否忽略
        /// </summary>
        /// <param name="owner">项拥有者</param>
        /// <param name="data">比对项</param>
        /// <param name="takedItems">已经忽略项</param>
        bool IsIgnore(IEnumerable<IDFR_ReadOnlyBattleFusionData.IItem> owner, IDFR_ReadOnlyBattleFusionData.IItem data, IReadOnlyCollection<IDFR_ReadOnlyBattleFusionData.IItem> takedItems);
    }

    [Implement(typeof(IDFR_BattleEffectFilter))]
    internal sealed class DFR_BattleEffectFilter : IDFR_BattleEffectFilter {

        public bool IsIgnore(IEnumerable<IDFR_ReadOnlyBattleFusionData.IItem> owner, IDFR_ReadOnlyBattleFusionData.IItem data, IReadOnlyCollection<IDFR_ReadOnlyBattleFusionData.IItem> takedItems) {
            var model = BattleSkillServiceBase.Instance.GetEffectModel(data.EntityInstanceID);
            if (model == null) {
                return false;
            }
            var result = true;
            if (result && model.IsUnique) {
                result = CheckUnique(owner, data, takedItems);
            }
            return !result;
        }

        private bool CheckUnique(IEnumerable<IDFR_ReadOnlyBattleFusionData.IItem> owner, IDFR_ReadOnlyBattleFusionData.IItem data, IReadOnlyCollection<IDFR_ReadOnlyBattleFusionData.IItem> takedItems) {
            return !takedItems.Any(item => item.EntityInstanceID.ID == data.EntityInstanceID.ID);
        }
    }
}
